package com.ayi.dao.userdao;

import com.ayi.bean.User;
import com.ayi.dao.cartdao.AddCart;
import com.ayi.utils.GetConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Ayingzz
 * @create 2021/11/28 13:41
 */
public class AddUser {

    /**
     * 添加一个账号
     * @param user
     * @return
     */
    public boolean addUser(User user) {
        Connection conn = new GetConn().getConnection();
        String sql = "insert into Users(username, password, isAdmin, isStop) value(?, ?, ?, ?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setBoolean(3, user.isAdmin());
            ps.setBoolean(4, user.isStop());
            ps.execute();
            // 创建用户同时创建购物车
            new AddCart().addCart(user.getUsername());
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

}
